package com.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.pojo.Store;
import com.shopping.service.store.StoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class StoreController {

    @Autowired
    StoreServiceImpl storeService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = {"/store/registerStore"})
    public Map<String, Object> registerStore(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        String idCard = jsonObject.getString("idCard");
        String storeName = jsonObject.getString("storeName");
        String avatar = jsonObject.getString("avatar");
        Map<String, Object> map = storeService.selectByName(storeName);
        Map<String, Object> result = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        if(Integer.parseInt(String.valueOf(map.get("code"))) == 0){
            if(map.get("store") != null){
                result.put("code", ResponseCode.ENTITY_ALREADY_EXISTS.getValue());
                return result;
            }
        }else{
            result.put("code", ResponseCode.SERVER_ERROR.getValue());
            return result;
        }
        Store store = new Store();
        store.setStoreName(storeName);
        store.setStoreAvatar(avatar);
        store.setLevel(1);
        store.setFans(0);
        return storeService.registerStore(redisUserId, idCard, store);
    }

    @PostMapping(value = {"/store/showAllStores"})
    public Map<String, Object> showAllStores(){
        return storeService.showAllStores();
    }

    @PostMapping(value = {"/store/showStoresByName"})
    public Map<String, Object> showStoresByName(@RequestBody JSONObject jsonObject){
        String storeName = jsonObject.getString("storeName");
        return storeService.showStoreByName(storeName);
    }

    @PostMapping(value = {"/store/showStore"})
    public Map<String, Object> showStore(@RequestBody JSONObject jsonObject){
        int storeId = Integer.parseInt(jsonObject.getString("storeId"));
        return storeService.showStoreById(storeId);
    }

    @PostMapping(value = {"/store/exchangeName"})
    public Map<String, Object> exchangeNameMessage(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        int storeId = Integer.parseInt(jsonObject.getString("storeId"));
        String name = jsonObject.getString("name");
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        Map<String, Object> result = storeService.selectStoreIdByUserId(redisUserId);
        if(result.get("code").equals(ResponseCode.REQUEST_SUCCESSED.getValue())){
            int selectStoreId = Integer.parseInt(String.valueOf(result.get("storeId")));
            if(selectStoreId == storeId){
                return storeService.exchangeStoreName(storeId, name);
            }
        }
        map.put("code", ResponseCode.SERVER_ERROR.getValue());
        return map;
    }

//    @PostMapping(value = {"/store/exchangeAvatar"})
//    public Map<String, Object> exchangeAvatarMessage(int storeId, byte[] avatar){
//        return storeService.exchangeStoreAvatar(storeId, avatar);
//    }

    @PostMapping(value = {"/store/addFans"})
    public Map<String, Object> addFans(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        int storeId = Integer.parseInt(jsonObject.getString("storeId"));
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return storeService.addFans(storeId, redisUserId);
    }

    @PostMapping(value = {"/store/showAllTrades"})
    public Map<String, Object> showAllTrades(@RequestBody JSONObject jsonObject){
        int storeId = Integer.parseInt(jsonObject.getString("storeId"));
        return storeService.showAllTrade(storeId);
    }

}
